package edu.cuit.springboot201.service;

import cn.hutool.core.util.StrUtil;
import edu.cuit.springboot201.dao.ScoreDao;
import edu.cuit.springboot201.model.StudentScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScoreService {
    @Autowired
    @Qualifier("scoreDaoFileImpl")
    private ScoreDao scoreDao;

    public void saveScore(StudentScore score) {
        scoreDao.saveScore(score);
    }

    public List<StudentScore> queryScore(String stuNo, String course) {
        List<StudentScore> scoreList = scoreDao.loadScore();
        List<StudentScore> result = scoreList.stream().filter(score -> {
            String stuNoTrim = StrUtil.trimToNull(stuNo);
            String courseTrim = StrUtil.trimToNull(course);
            if (stuNoTrim == null && courseTrim == null) {
                return true;
            }
            if (stuNoTrim != null && courseTrim == null) {
                return score.getStuNo().equals(stuNoTrim);
            }
            if (stuNoTrim == null && courseTrim != null) {
                return score.getCourse().equals(courseTrim);
            }
            return score.getStuNo().equals(stuNoTrim) && score.getCourse().equals(courseTrim);
        }).collect(Collectors.toList());
        return result;
    }

}
